package com.five.calendar.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.five.calendar.R;
import com.five.calendar.database.bean.TargetBean;
import com.five.calendar.ui.widget.BaseViewHolder;
import com.five.calendar.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuxiaoxin on 2017/9/6.
 */
public class GoalListAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<TargetBean> mList;

    public GoalListAdapter(Context context, List<TargetBean> list) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(mInflater.inflate(R.layout.recyclerview_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        BaseViewHolder viewHolder = (BaseViewHolder) holder;
        long now = System.currentTimeMillis();
        String title = mList.get(position).title;
        viewHolder.setText(R.id.title, title);
        Log.e("ZXX", mList.get(position).isComplete + "");
        String time = TimeUtils.timeFromCreateTime(mList.get(position).endTime);
        viewHolder.setText(R.id.goal_time, time);
        if (mList.get(position).isComplete == 0 && mList.get(position).endTime > now) {
            viewHolder.setImageResource(R.id.goal_done, R.drawable.ic_goals_progress);
            viewHolder.setVisible(R.id.time_icon, true);
            viewHolder.setVisible(R.id.complete_lay, false);
            viewHolder.setBackgroundRes(R.id.card_lay, R.drawable.bg_schedules);
        } else if (mList.get(position).isComplete == 1){
            viewHolder.setImageResource(R.id.goal_done, R.drawable.ic_gals_done);
            viewHolder.setVisible(R.id.time_icon, false);
            viewHolder.setVisible(R.id.complete_lay, true);
            viewHolder.setBackgroundRes(R.id.card_lay, R.drawable.bg_schedules_green);
        } else if (mList.get(position).isComplete == 2){
            viewHolder.setImageResource(R.id.goal_done, R.drawable.ic_gals_done);
            viewHolder.setVisible(R.id.time_icon, true);
            viewHolder.setBackgroundRes(R.id.card_lay, R.drawable.bg_schedules_red);
        } else if (mList.get(position).isComplete == 0 && mList.get(position).endTime < now){
            //逾期
            viewHolder.setImageResource(R.id.goal_done, R.drawable.ic_gals_done);
            viewHolder.setVisible(R.id.time_icon, true);
            viewHolder.setBackgroundRes(R.id.card_lay, R.drawable.bg_schedules_red);
        }

        if (position == 0 && mList.get(position).isComplete == 0 && mList.get(position).endTime > now){
            viewHolder.setVisible(R.id.in_progress, true);
            viewHolder.setText(R.id.in_progress, R.string.inprogress);
        } else if (position == 0 && mList.get(position).isComplete == 1){
            viewHolder.setVisible(R.id.in_progress, true);
            viewHolder.setText(R.id.in_progress, R.string.completed);
        } else if (position == 0 && mList.get(position).isComplete == 2){
            viewHolder.setVisible(R.id.in_progress, true);
            viewHolder.setText(R.id.in_progress,R.string.overdue);
        } else if (position == 0 && mList.get(position).isComplete == 0 && mList.get(position).endTime < now){
            viewHolder.setVisible(R.id.in_progress, true);
            viewHolder.setText(R.id.in_progress, R.string.overdue);
        } else if ((position - 1 >= 0) && (mList.get(position -1).isComplete == 0) && (mList.get(position).isComplete == 1)){
            viewHolder.setVisible(R.id.in_progress, true);
            viewHolder.setText(R.id.in_progress, R.string.completed);
        } else if ((position - 1 >= 0) && ((mList.get(position - 1).isComplete == 1) || (mList.get(position - 1).isComplete == 0)) && (mList.get(position).isComplete == 2)){
            viewHolder.setVisible(R.id.in_progress,true);
            viewHolder.setText(R.id.in_progress, R.string.overdue);
        } else if ((position - 1 >= 0) && (((mList.get(position - 1).isComplete == 0) && (mList.get(position - 1).endTime > now)) || (mList.get(position - 1).isComplete == 1)) && ((mList.get(position).isComplete == 0) && mList.get(position).endTime < now)){
            viewHolder.setVisible(R.id.in_progress,true);
            viewHolder.setText(R.id.in_progress, R.string.overdue);
        } else {
            viewHolder.setVisible(R.id.in_progress, false);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void removeItem(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
    }
}

